home *** CD-ROM | disk | FTP | other *** search
/ EnigmA Amiga Run 1999 March / EnigmA AMIGA RUN 35 (1999)(G.R. Edizioni)(IT)[!][issue 1999-03].iso / earcd / devel / vbcc-68k-src / machines / amiga68k / libsrc / time / strftime.c < prev    next >
C/C++ Source or Header  |  1999-01-01  |  3KB  |  127 lines

  1. #include <time.h>
  2.  
  3. #include <libraries/locale.h>
  4.  
  5. #define ADDS(st)  tmp=strftime(s,maxsize-size,(st),timeptr);break;
  6.  
  7. #define ADDN(a,b) tmp=strfnumb(s,maxsize-size,(a),(b));break;
  8.  
  9. #define STOR(c)   if(++size<=maxsize)*s++=(c);
  10.  
  11. #define STR(a) (strings[(a)-1])
  12.  
  13.  
  14. /* All calendar strings */
  15. static const char *strings[]=
  16. { "Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday",
  17.   "Sun","Mon","Tue","Wed","Thu","Fri","Sat",
  18.   "January","February","March","April","May","June",
  19.   "July","August","September","October","November","December",
  20.   "Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec",
  21.   "","","AM","PM" };
  22.  
  23. static size_t strfnumb(char *s,size_t maxsize,signed int places,size_t value)
  24. { size_t size=0;
  25.   if(places>1)
  26.     size=strfnumb(s,maxsize,places-1,value/10);
  27.   else if(value>=10)
  28.     size=strfnumb(s,maxsize,places+1,value/10);
  29.   else
  30.     while ((places++<-1) && (++size<=maxsize)) s[size-1]=' ';
  31.   if(++size<=maxsize)
  32.     s[size-1]=(value%10+'0');
  33.   return size;
  34. }
  35.  
  36. size_t strftime(char *s,size_t maxsize,const char *format,const struct tm *timeptr)
  37. { size_t size=0,tmp;
  38.   while(*format)
  39.   { if(*format=='%')
  40.     { tmp=0;
  41.       switch(*++format)
  42.       { case 'a':
  43.           ADDS(STR(ABDAY_1+timeptr->tm_wday));
  44.         case 'b':
  45.         case 'h':
  46.           ADDS(STR(ABMON_1+timeptr->tm_mon));
  47.         case 'c':
  48.           ADDS("%m/%d/%y");
  49.         case 'd':
  50.           ADDN(2,timeptr->tm_mday);
  51.         case 'e':
  52.           ADDN(-2,timeptr->tm_mday);
  53.         case 'j':
  54.           ADDN(3,timeptr->tm_yday+1);
  55.         case 'k':
  56.           ADDN(-2,timeptr->tm_hour);
  57.         case 'l':
  58.           ADDN(-2,timeptr->tm_hour%12+(timeptr->tm_hour%12==0)*12);
  59.         case 'm':
  60.           ADDN(2,timeptr->tm_mon+1);
  61.         case 'p':
  62.           ADDS(STR(AM_STR+(timeptr->tm_hour>=12)));
  63.         case 'r':
  64.           ADDS("%I:%M:%S %p");
  65.         case 'w':
  66.           ADDN(1,timeptr->tm_wday);
  67.         case 'x':
  68.           ADDS("%m/%d/%y %H:%M:%S");
  69.         case 'y':
  70.           ADDN(2,timeptr->tm_year%100);
  71.         case 'A':
  72.           ADDS(STR(DAY_1+timeptr->tm_wday));
  73.         case 'B':
  74.           ADDS(STR(MON_1+timeptr->tm_mon));
  75.         case 'C':
  76.           ADDS("%a %b %e %H:%M:%S %Y");
  77.         case 'D':
  78.           ADDS("%m/%d/%y");
  79.         case 'H':
  80.           ADDN(2,timeptr->tm_hour);
  81.         case 'I':
  82.           ADDN(2,timeptr->tm_hour%12+(timeptr->tm_hour%12==0)*12);
  83.         case 'M':
  84.           ADDN(2,timeptr->tm_min);
  85.         case 'R':
  86.           ADDS("%H:%M");
  87.         case 'S':
  88.           ADDN(2,timeptr->tm_sec);
  89.         case 'T':
  90.         case 'X':
  91.           ADDS("%H:%M:%S");
  92.         case 'U':
  93.           ADDN(2,(timeptr->tm_yday+7-timeptr->tm_wday)/7);
  94.         case 'W':
  95.           ADDN(2,(timeptr->tm_yday+7-(6+timeptr->tm_wday)%7)/7);
  96.         case 'Y':
  97.           ADDN(4,timeptr->tm_year+1900);
  98.         case 't':
  99.           STOR('\t');
  100.           break;
  101.         case 'n':
  102.           STOR('\n');
  103.           break;
  104.         case '%':
  105.           STOR('%');
  106.           break;
  107.         case '\0':
  108.           format--;
  109.           break;
  110.       }
  111.       size+=tmp;
  112.       s+=tmp;
  113.     }
  114.     else
  115.       STOR(*format);
  116.     format++;
  117.   }
  118.   STOR('\0');
  119.   if(size>maxsize)
  120.   { s-=size;
  121.     if(maxsize) /* Don't know if this is necessary, therefore it's here ;-) */
  122.       s[maxsize-1]='\0';
  123.     size=1;
  124.   }
  125.   return size-1;
  126. }
  127.